package com.xintonghua.bussiness.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.adapter.BaseFragmentAdapter;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseFragment;
import com.xintonghua.bussiness.bean.StoreType;
import com.xintonghua.bussiness.ui.fragment.cube.CheckingInFragment;
import com.xintonghua.bussiness.ui.fragment.cube.ClientServerFragment;
import com.xintonghua.bussiness.ui.fragment.cube.RepertoryFragment;
import com.xintonghua.bussiness.ui.fragment.cube.TurnoverFragment;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.picker.SinglePicker;

/**
 * 数据魔方
 * 作者：wanxianyi
 * 时间：2018/1/11.
 * 包名：com.xintonghua.bussiness.ui.fragment
 */

public class DataCubeFragment extends BaseFragment {
    @BindView(R.id.tab_cube_type)
    TabLayout tabCubeType;
    @BindView(R.id.tv_select_store)
    TextView tvSelectStore;
    @BindView(R.id.vp_cube)
    ViewPager vpCube;
    Unbinder unbinder;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titleList;
    private String[] title = new String[]{"财务分析", "客户", "库存", "出勤记录"};
    private BaseFragmentAdapter adapter;
    private TurnoverFragment turnoverFragment = new TurnoverFragment();
    private ClientServerFragment clientServerFragment = new ClientServerFragment();
    private RepertoryFragment repertoryFragment = new RepertoryFragment();
    private CheckingInFragment checkingInFragment = new CheckingInFragment();
    List<StoreType> storeTypeList;

    @Override
    public int getLayout() {
        return R.layout.fragment_data_cube;
    }

    @Override
    public void initUI(View view) {
        unbinder = ButterKnife.bind(this, view);
        titleList = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putInt(BuildConfig.FLAVOR, AuthManager.geteAuth().getShop_id());
        turnoverFragment.setArguments(bundle);
        clientServerFragment.setArguments(bundle);
        repertoryFragment.setArguments(bundle);
        checkingInFragment.setArguments(bundle);

        fragmentList.add(turnoverFragment);
        fragmentList.add(clientServerFragment);
        fragmentList.add(repertoryFragment);
        fragmentList.add(checkingInFragment);
        for (String s : title) {
            titleList.add(s);
        }
        adapter = new BaseFragmentAdapter(getChildFragmentManager(), fragmentList, titleList);
        vpCube.setAdapter(adapter);
        tabCubeType.setupWithViewPager(vpCube);
        httpCent.shopType(this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                storeTypeList = JSONObject.parseArray((String) data, StoreType.class);
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        tabCubeType.post(new Runnable() {
            @Override
            public void run() {
                MyUtils.setTabLine(getContext(), tabCubeType, 10, 10);
            }
        });
    }

    private void showTaskChoose(final List<StoreType> pList) {
        List<String> list = new ArrayList<>();
        for (StoreType type : pList) {
            list.add(type.getName());
        }
        SinglePicker<String> picker = new SinglePicker<>(getActivity(), list);
        picker.setCanLoop(false);//不禁用循环
        picker.setLineVisible(true);
        picker.setShadowVisible(true);
        picker.setTextSize(18);
        picker.setSelectedIndex(1);
        picker.setWheelModeEnable(true);
        //启用权重 setWeightWidth 才起作用
        picker.setLabel(null);
        picker.setWeightEnable(true);
        picker.setWeightWidth(1);
        picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
        picker.setUnSelectedTextColor(0xFF999999);
        picker.setOnItemPickListener(new OnItemPickListener<String>() {
            @Override
            public void onItemPicked(int index, String item) {
                for (StoreType type : storeTypeList) {
                    if (type.getName().equals(item)) {
                        tvSelectStore.setText(item);
                        // AuthManager.geteAuth().setShop_id(type.getId());
                        fragmentList.clear();
                        titleList = new ArrayList<>();
                        Bundle bundle = new Bundle();
                        bundle.putInt(BuildConfig.FLAVOR, type.getId());
                        turnoverFragment.setArguments(bundle);
                        clientServerFragment.setArguments(bundle);
                        repertoryFragment.setArguments(bundle);
                        checkingInFragment.setArguments(bundle);
                        fragmentList.add(turnoverFragment);
                        fragmentList.add(clientServerFragment);
                        fragmentList.add(repertoryFragment);
                        fragmentList.add(checkingInFragment);
                        for (String s : title) {
                            titleList.add(s);
                        }
                        adapter = new BaseFragmentAdapter(getChildFragmentManager(), fragmentList, titleList);
                        vpCube.setAdapter(adapter);
                        tabCubeType.setupWithViewPager(vpCube);
                        return;
                    }
                }

            }
        });
        picker.show();
    }

    @OnClick({R.id.tv_select_store})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_select_store:
                showTaskChoose(storeTypeList);
                break;
            case R.id.tv_xiaohuo:

                break;
            case R.id.tv_jinghuoshu:
                break;
        }


    }


}
